package com.brzjomo.secretiveworld.object.block;

import java.util.Random;
import java.util.stream.Stream;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.HorizontalBlock;
import net.minecraft.block.material.PushReaction;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.state.DirectionProperty;
import net.minecraft.state.StateContainer.Builder;
import net.minecraft.util.Direction;
import net.minecraft.util.Mirror;
import net.minecraft.util.Rotation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.shapes.IBooleanFunction;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.shapes.VoxelShapes;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

public class StoneBrazierBlock extends Block {

	public static final DirectionProperty FACING = HorizontalBlock.HORIZONTAL_FACING;

	public StoneBrazierBlock(Properties properties) {
		super(properties);
		this.setDefaultState(this.stateContainer.getBaseState().with(FACING, Direction.NORTH));
	}

	private static final VoxelShape SHAPE_N = Stream.of(Block.makeCuboidShape(4, 0, 4, 12, 16, 12),
			Block.makeCuboidShape(5, 0, 12, 11, 16, 13), Block.makeCuboidShape(5, 0, 3, 11, 16, 4),
			Block.makeCuboidShape(3, 0, 5, 4, 16, 11), Block.makeCuboidShape(12, 0, 5, 13, 16, 11)).reduce((v1, v2) -> {
				return VoxelShapes.combineAndSimplify(v1, v2, IBooleanFunction.OR);
			}).get();

	@Override
	public VoxelShape getShape(BlockState stateIn, IBlockReader worldIn, BlockPos posIn, ISelectionContext contextIn) {
		switch (stateIn.get(FACING)) {
		case NORTH:
			return SHAPE_N;
		case EAST:
			return SHAPE_N;
		case SOUTH:
			return SHAPE_N;
		case WEST:
			return SHAPE_N;
		default:
			return SHAPE_N;
		}
	}

	public BlockState getStateForPlacement(BlockItemUseContext context) {
		BlockState blockstate = this.getDefaultState();
		blockstate = blockstate.with(FACING, context.getPlacementHorizontalFacing().getOpposite());
		return blockstate;
	}

	@Override
	protected void fillStateContainer(Builder<Block, BlockState> builder) {
		builder.add(FACING);
	}

	@OnlyIn(Dist.CLIENT)
	public void animateTick(BlockState stateIn, World worldIn, BlockPos posIn, Random randIn) {
		double d0 = (double) posIn.getX() + 0.5D;
		double d1 = (double) posIn.getY() + 0.9D;
		double d2 = (double) posIn.getZ() + 0.5D;
		worldIn.addParticle(ParticleTypes.FLAME, d0, d1, d2, 0, 0, 0);
		worldIn.addParticle(ParticleTypes.SMOKE, d0, d1, d2, 0, 0, 0);
	}

	// 旋转和镜像支持
	public BlockState rotate(BlockState state, Rotation rot) {
		return state.with(FACING, rot.rotate(state.get(FACING)));
	}

	public BlockState mirror(BlockState state, Mirror mirrorIn) {
		return state.rotate(mirrorIn.toRotation(state.get(FACING)));
	}

	// 活塞推动支持
	public PushReaction getPushReaction(BlockState state) {
		return PushReaction.NORMAL;
	}

}
